
// ---------------------------ajax全局配置------------------------
//设置Ajax 异步请求
import Cookie from './cookie.js'
import Api from  './api.js'
import WApi from  '../wj/api.js'
import Base_api from './Base_api.js'
import Basefunction from './Basefunction.js'

$.ajaxSetup({
    dataType:'json',
    async:false,
})

// --------------------------用户查询的api------------------------
let baseurl='http://localhost:8080/'



// --------------------用于显示的文本不修改的信息{{}}-----------------
var tupian=''                           //餐厅上传后图片地址
let getmsg=[]                            //餐馆下拉框

// --------------------------主要内容------------------------
let vue=new Vue({
    el:'#release',
    data:{
        applyhost:false,
        carezh:[],                              //关注的账号
        fanszh:[],                              //账号的粉丝
        uinfos:[],                              //当前用户
        search:'',                               //搜索框内容
        title:'',                               //活动标题
        price:'',                             //活动价格
        people:'',                            //报名人数
        begin_time:'',                       //活动时间
        end_time:'',                        //截至报名时间
        RadioGroup1:'',                    //活动玩法
        checkedNames:[],                  //活动标签
        updateInfo:'',                   //修改图片的文本域
        addInfo:'',                     //添加图片的文本域
        require:'',                    //活动内容
        checked:false,                //同意协议状态
        name:'空',                      //姓名
        value:'',                           //餐厅下拉框的值
        options: getmsg,                  //餐厅下拉框

        imgUrl:'',
        imgUr2:'',
    },
    methods:{
        //用户进行发布饭局的时候必须要 合法性检查是否有资格
        async applyHost() {
            this.applyhost = await Base_api.getapplyHost(this.uinfos.zh)
            if (this.applyhost == false) {
                this.$alert('请先申请host资格', '系统提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'warning',
                            message: '即将跳转到申请界面...'
                        });
                        location.href = "./applyhost.html"
                    }
                });
            }
        },
        //获取个人信息
        async info () {
            let zh = sessionStorage.getItem("zh")
            let allinfo = await WApi.login()
            for(let i=0;i<allinfo.length;i++){
                if(allinfo[i].zh==zh){
                    this.uinfos=allinfo[i]
                    break
                }
            }
            //调用关注表的内容
            let careinfo = await WApi.careInfo()
            let caremsg = careinfo.data
            //获取
            for (let i=0;i<caremsg.length;i++){
                for (let j=0;j<allinfo.length;j++){
                    //关注的账号
                    if(caremsg[i].user_zh==zh){
                        if(caremsg[i].care_zh==allinfo[j].zh){
                            this.carezh.push(allinfo[j])
                        }
                    }
                    //粉丝的账号
                    if(caremsg[i].care_zh==zh){
                        if(caremsg[i].user_zh==allinfo[j].zh){
                            this.fanszh.push(allinfo[j])
                        }
                    }
                }
            }
            this.applyHost()
        },
        deleteImg(){                            //删除图片功能
            $('#ImgPr').attr("src","")
        },
        revoke(){                               //取消按钮
            location.href='Release.html'
        },
        async commit() {                              //提交按钮

            await addImg()
            // alert(document.getElementById('up').files[0])
            if(document.getElementById('up').files[0]){
                await upImg()
            }
           if(new Date(this.begin_time)<new Date()){
               this.begin_time = new Date()

           }
            if(new Date(this.begin_time).getTime()+60000*30> new Date(this.end_time).getTime()){
                this.$alert('活动申请持续时间不得小于1小时', '系统提示', {
                    confirmButtonText: '确定',
                });
                return
            }
            console.log(this.title + this.price + this.people + this.RadioGroup1 + this.checkedNames + this.begin_time + this.end_time + this.updateInfo + this.addInfo + this.require)
            alert('123')
            if (this.checked == true && this.title != '' && this.people != '' && this.begin_time != '' && this.end_time != '') {
                if (this.imgUrl == null) {
                    this.$message({
                        type: 'error',
                        message: '图片超过最大限制或者图片重复或者没有图片'
                    });
                    return
                    if (this.imgUrl == null) {
                        this.$message({
                            type: 'error',
                            message: '图片超过最大限制或者图片重复或者没有图片'
                        });
                    }
                }

                let a = this.checkedNames             //------将活动标签集合类型转换称字符串

                //----------------------------提交数据创建饭局-----------------------
                $.post(baseurl + 'ActivityServlet', {
                    "type": 'addActivity',
                    "title": this.title,             //活动标题
                    "price": this.price,             //活动单价
                    "max_people": this.people,       //最大参与人数
                    "wanfa": this.RadioGroup1,       //活动玩法
                    "tag": a.toString(),        //活动标签
                    "img": this.imgUrl,              //饭局活动图片地址
                    "push_id": this.uinfos.id,              //发布人id
                    "c_time": formatDateTime(this.begin_time),       //开始时间
                    "end_time": formatDateTime(this.end_time),       //截至时间
                    "ct_name": getmsg[this.value - 1].label,    //餐厅名字
                    "address": getmsg[this.value - 1].address,   //地点
                    "lb_name": getmsg[this.value - 1].type,   //地点
                    "text": this.require
                }, function (data) {
                    if(data.success ==true){
                        location.href="./Release.html"
                    }
                }, "json")

            } else {
                // alert(getmsg[this.value-1].label)
                this.$alert('没有勾选同意或是没有填写完整', '系统提示', {
                    confirmButtonText: '确定',
                });
            }
        },
    },
    mounted(){
        if(sessionStorage.getItem("zh")==null){
            this.$alert('请先登录', '系统提示', {
                confirmButtonText: '确定',
                callback: action => {
                    this.$message({
                        type: 'warning',
                        message: '即将跳转到登录界面...'
                    });
                    location.href='./login.html'
                }
            });
        }else {
            this.info()
            // -----------------获取餐厅信息方法-------------------
            getmsg=canting()
        }

    }
})


// --------------------------自定义功能------------------------
// 日期格式转换     yyyy-MM-dd HH:mm:ss
var formatDateTime = function (date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    var minute = date.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    var s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+s;
};

//餐厅类
function ct() {
    var value
    var label
    var address
    var type
}
//获取餐厅的信息
function canting() {

    $.post(baseurl+'/RestaurantServlet',{type:'selectAll'},function (result) {
        let list;
        list=result.data
        // alert(JSON.stringify(list))
        for (let i=0;i<list.length;i++){
            let a =new ct()
            a.value=list[i].id
            a.label=list[i].name
            a.address=list[i].place
            a.type=list[i].type
            getmsg[i]=a
        }
    })
    // alert(JSON.stringify(getmsg))
    return getmsg
}
//图片上传
function upImg() {
    $.ajax({
        url: "http://81.68.126.120:8089/uploadapi/",
        data: new FormData($("#form1")[0]),
        type: "post",
        //cache: false,//上传文件无需缓存
        processData: false,//用于对data参数进行序列化处理 这里必须false
        contentType: false, //必须
        success: function (result) {
            if(result.code == 0){
                vue.imgUr2=result.data
            }else if(result.code ==2){
                vue.imgUr2=result.message
            }else {
                vue.imgUr2=null
            }

            // alert('上传方法的回调up'+tupian)
        }
    })
}

async function addImg() {
    // alert("zhixing")
    await $.ajax({
        url: "http://81.68.126.120:8089/uploadapi/",
        data: new FormData($("#form2")[0]),
        type: "post",
        //cache: false,//上传文件无需缓存
        processData: false,//用于对data参数进行序列化处理 这里必须false
        contentType: false, //必须
        success: function (result) {
            let msg = result
            if(msg.code == 0){
                vue.imgUrl=msg.data
                // alert('0')
            }else if(msg.code ==2){
                vue.imgUrl=msg.message
                // alert('2')
            }else {
                vue.imgUrl=null
            }

            // alert('上传方法的回调add'+tupian)
        }
    })
}


//初始化
function initData() {
    WApi.setCookie('name','Release发来的','s20')

}
function getUser() {
    // alert(this.name)
    this.name=WApi.getCookie('name')
    // alert(this.name)
}


//str:时间单位 s秒、h时、d天、m分
function getsec(str) {
    var str1 = str.substring(1, str.length) * 1;
    var str2 = str.substring(0, 1);
    if(str2 == "s") {
        return str1 * 1000;
    } else if(str2 == "h") {
        return str1 * 60 * 60 * 1000;
    } else if(str2 == "d") {
        return str1 * 24 * 60 * 60 * 1000;
    }else if(str2 == "m") {
        return str1 *  60 * 1000;
    }
}
